package com.mp.mp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mp.mp.dao.UserMapper;
import com.mp.mp.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

@Controller
public class DateController {

    @Resource
    HttpServletResponse httpServletResponse;

    @Resource
    UserMapper userMapper;


    @RequestMapping("/stu")
    @ResponseBody
    public Object findDate() {
        QueryWrapper queryWrapper = new QueryWrapper();
        List userList = userMapper.selectList(queryWrapper);
        return userList;
    }

    @RequestMapping("/stuadd")
    @ResponseBody
    public Object findDateadd() {
//        System.out.println(("----- selectAll method test ------"));
        QueryWrapper queryWrapper = new QueryWrapper();
        User u = new User();
        u.setAge(new Random().nextInt(20) % (11) + 10);
        u.setEmail("gcr123@163.com");
        u.setName("test");
        int s = userMapper.insert(u);
        return s;

    }

    @RequestMapping("/stude")
    @ResponseBody
    public Object findDatede() {

        //不需要写mapper xml文件
        int s = userMapper.deleteById(2);
//        queryWrapper.select("name,age");//查询特定的列
        return s;

    }

    @RequestMapping("/studemap")
    @ResponseBody
    public Object findDatedemap() {
        Map m = new HashMap();
        m.put("name", "Tom");
        //我擦。方便啊再也不需要去xml配置sql语句了
        //不需要写mapper xml文件
        int s = userMapper.deleteByMap(m);
        return s;
    }

    @RequestMapping("/studew")
    @ResponseBody
    public Object findDatedew(String name) {
        /**
         * <p>
         * 根据 entity 条件，删除记录
         * </p>
         *
         * @param queryWrapper 实体对象封装操作类（可以为 null）
         * @return 删除成功记录数
         */
        Map m = new HashMap();
        m.put("name", name);
        QueryWrapper ueryWrapper = new QueryWrapper(m);
        int s = userMapper.delete(ueryWrapper);
        return s;
    }

    @RequestMapping("/dedatalist")
    @ResponseBody
    public Object findDatedelist() {
        List ls = new ArrayList();
        ls.add(4);
        ls.add(5);
        int s = userMapper.deleteBatchIds(ls);
        return s;
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object findDateupdate() {
        List ls = new ArrayList();
        User tu = userMapper.selectById(16);
        tu.setRoleid(2);
        tu.setName("update");
        int s = userMapper.updateById(tu);
        return s;
    }

    @RequestMapping("/update2")
    @ResponseBody
    public Object findDateupdate2() {
        User tu = userMapper.selectById(16);
        tu.setRoleid(2);
        tu.setName("sssqqs");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",12);
        int s = userMapper.update(tu,queryWrapper);
        return s;
    }

    @RequestMapping("/find1")
    @ResponseBody
    public Object findDate1() {
        User tu = userMapper.selectById(16);
        return tu;
    }

    @RequestMapping("/find2")
    @ResponseBody
    public Object findDate2() {
        List ls = new ArrayList();
        ls.add(14);
        ls.add(15);
        List tu = userMapper.selectBatchIds(ls);
        return tu;
    }

    @RequestMapping("/find3")
    @ResponseBody
    public Object findDate3() {
        Map map = new HashMap();
        map.put("name","a");
        List tu = userMapper.selectByMap(map);
        return tu;
    }

    @RequestMapping("/find4")
    @ResponseBody
    public Object findDate4() {
//        Map map = new HashMap();
//        map.put("name","a");
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id",15);
        User tu = userMapper.selectOne(queryWrapper);
        return tu;
    }

    @RequestMapping("/find5")
    @ResponseBody
    public Object findDate5() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("age",15);
        Integer tu = userMapper.selectCount(queryWrapper);
        return tu;
    }


}
